function post(url, requestData, requestHeader) {
    return new Promise((resolve, reject) => {
        let api_resp = {}
        console.log("url:", url, "requestData:", requestData, "requestHeader:", requestHeader)
        const csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;
        fetch(url, {
            method: 'POST',
            headers: requestHeader || {
                'Content-Type': 'application/json',
                'X-CSRFToken': csrftoken
            },
            body: JSON.stringify(requestData)
        })
            .then(response => response.json())
            .then(data => {
                console.log("响应数据", data)
                if ('code' in data && data.code === 0) {
                    api_resp = new_success(null, data.data)
                } else {
                    api_resp = new_fail(data.code, data.msg, data.data)
                }
                resolve(api_resp);
            })
            .catch(error => {
                console.error('Error:', error);
                api_resp = new_error();
                reject(api_resp);
            });
    });
}

function get(url, requestData, requestHeader) {
    console.log("url:", url, "requestData:", requestData, "requestHeader:", requestHeader)

    fetch(url, {
        method: 'GET',
        headers: requestHeader || {
            'Content-Type': 'application/json',
            'X-CSRFToken': csrftoken
        },
    })
        .then(response => response.json())
        .then(menuData => {
            if ('code' in menuData && menuData.code === 0) {
                resultData = new_success(menuData = menuData.data)
            } else {
                resultData = new_fail(menuData.code, menuData.msg, menuData.data)
            }
        })
        .catch(error => {
            console.error('Error:', error);
            resultData = new_error()
        });
}


function new_success(msg, respdata) {
    return {
        code: 0,
        msg: msg || "操作成功",
        data: respdata || null
    }
}

function new_error(code, msg, respdata) {
    return {
        code: code || 900001,
        msg: msg || "系统异常,请联系管理员",
        data: respdata || null
    }
}

function new_fail(code, msg) {
    return {
        code: code || 900000,
        msg: msg || "操作失败",
        data: null
    }
}